package cs2340.droidceptiontodo;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;


/**
 * Used to display and interact with a list of ToDoItems
 */
public class ToDoListAdapter extends BaseAdapter {
	private final Activity context;
	private final List<ToDoItem> items;
	
	/**
	 * Creates a new adapter
	 * @param context	The context in which to display the ToDoItems
	 * @param items		The list of ToDoItems to display
	 */
	public ToDoListAdapter(Activity context, List<ToDoItem> items) {
		this.context = context;
		this.items = items;
	}
	
	static class ViewHolder {
		protected TextView name;
		protected TextView category;
		protected TextView duedate;
		protected Button viewButton;
		protected CheckBox checkbox;
		protected String id; // Used to find item selected for detail view
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if(convertView == null) { // Create a new view
			// Get the todo_item view (todo_item.xml)
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.todo_item, null);
			final ViewHolder viewHolder = new ViewHolder();
			
			// Get the TextViews from the todo_item view
			viewHolder.name = (TextView) view.findViewById(R.id.name);
			viewHolder.category = (TextView) view.findViewById(R.id.category);
			viewHolder.duedate = (TextView) view.findViewById(R.id.duedate);
			
			// Get view button and add a listener
			viewHolder.viewButton = (Button) view.findViewById(R.id.view);
			viewHolder.viewButton.setOnClickListener(new OnClickListener() {
				//Displays detailed view of to-do item
				@SuppressWarnings("unused")
				@Override
				public void onClick(View arg0) {
					/* Should always find to-do item clicked
					 * by current ID
					 */
				
					ToDoItem item = new ToDoItem(); // all fields null, checked = false
					for (int i = 0; i < items.size(); i++) {
						if(items.get(i).getID().equals(viewHolder.id)) {
							item = items.get(i);
							Log.d("Debug","Item found for detail view!");
						}
					}
					// set the item in the detail view
					ViewToDoItemActivity.item = item;
					// Start detail view activity
					Intent testIntent = new Intent(context, ViewToDoItemActivity.class); 
					context.startActivity(testIntent);
				}
				
			});
			
			// Get checkbox and add a listener
			viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox);
			viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// Get the ToDoItem and set isChecked
					ToDoItem element = (ToDoItem) viewHolder.checkbox.getTag();
					element.setChecked(buttonView.isChecked());
					// TODO Determine how to handle category color
					if(element.isChecked()) {
						viewHolder.name.setTextColor(Color.GRAY);
						//viewHolder.category.setTextColor(Color.DKGRAY);
						viewHolder.duedate.setTextColor(Color.DKGRAY);
					}
					else {
						viewHolder.name.setTextColor(Color.WHITE);
						//viewHolder.category.setTextColor(Color.LTGRAY);
						viewHolder.duedate.setTextColor(Color.LTGRAY);
					}
					
					// Save the toDoItem
					//Database itemUpdater = new Database(context);
					Database itemUpdater = Database.getInstance(context);
					itemUpdater.open();
					itemUpdater.updateItem(element);
					itemUpdater.close();
				}
			});
			
			view.setTag(viewHolder);
			viewHolder.checkbox.setTag(items.get(position));
		}
		
		else { // Use the view that's already there
			view = convertView;
			((ViewHolder) view.getTag()).checkbox.setTag(items.get(position));
		}
		
		// Add appropriate values to each of the elements
		ToDoItem item = items.get(position);
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.name.setText(item.getName());
		holder.category.setText(item.getCategory().getName());
		holder.duedate.setText(item.getDueDate()
				+ (item.getDueTime().equals("")? "" : "@"+item.getDueTime()));
		holder.checkbox.setChecked(item.isChecked());
		holder.id = item.getID();
		return view;
	}

}
